﻿using System;
using System.ServiceProcess;
using System.Configuration.Install;

namespace Common
{
    /// <summary>
    /// 系统服务注册，反注册类
    /// </summary>
    public class WinServiceHelper
    {
        public string _strLogSrvName;
        public WinServiceHelper(string strLogSrvName)
        {
            _strLogSrvName = "ZGOfficeService";
        }
        
        public bool IsSrvExisted(string strSrvName_)
        {
            try
            {
                ServiceController srvCtrl = new ServiceController(strSrvName_);
                ServiceControllerStatus euStatu = srvCtrl.Status;
                return true;
            }
            catch (SystemException) { }

            return false;
        }

        public void Install(string strSrvFile_)
        {
            if (IsSrvExisted(_strLogSrvName))
            {
                return;
            }

            System.Collections.Hashtable hashState = new System.Collections.Hashtable();
            AssemblyInstaller asInst = new AssemblyInstaller();
            asInst.UseNewContext = true;
            asInst.Path = strSrvFile_;
            asInst.Install(hashState);
            asInst.Commit(hashState);
        }

        public void Uninstall(string strSrvFile_)
        {
            if (IsSrvExisted(_strLogSrvName))
            {
                AssemblyInstaller asInst = new AssemblyInstaller();
                asInst.UseNewContext = true;
                asInst.Path = strSrvFile_;
                asInst.Uninstall(null);
            }
        }
    }
}
